package cn.ecnu.controller;

import cn.ecnu.common.utils.JwtToken;
import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.ConsultRecordMapper;
import cn.ecnu.mapper.ConsultantMapper;
import cn.ecnu.mapper.SupervisorMapper;
import cn.ecnu.pojo.ConsultRecord;
import cn.ecnu.pojo.Consultant;
import cn.ecnu.pojo.Supervisor;
import cn.ecnu.service.ConsultantService;
import cn.ecnu.vo.Converter;
import cn.ecnu.vo.fromFront.*;
import cn.ecnu.vo.toFront.ConsultRecordVo;
import cn.ecnu.vo.toFront.ConsultantVo;
import cn.ecnu.vo.toFront.ConsultantWithFlagVo;
import cn.ecnu.vo.toFront.SupervisorVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static cn.ecnu.common.constant.BusinessConstant.CONSULTANT_DEFAULT_DUTY_KEY;

@RestController
@RequestMapping("/consultant")
public class ConsultantController {

    @Resource
    private ConsultantMapper consultantMapper;

    @Resource
    private ConsultantService consultantService;

    @Resource
    private ConsultRecordMapper consultRecordMapper;

    @Resource
    private SupervisorMapper supervisorMapper;

    @Resource
    private Converter converter;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/getOnlineConsultants/{id}")
    //得带有是否咨询过的 flag
    public R getOnlineConsultants(@PathVariable("id") Integer id) {

        try {

            List<Consultant> consultants = consultantMapper.getAllConsultants();

            //筛选出在线的那些咨询师
            ArrayList<ConsultantVo> onlineConsultants = new ArrayList<>();
            for (Consultant consultant : consultants) {
                Integer consultantId = consultant.getConsultantId();
                if (consultantService.judgeOnline(consultantId)) {
                    onlineConsultants.add(converter.getConsultantVo(consultantId));
                }
            }

            //加上是否咨询过的 flag
            List<ConsultantWithFlagVo> result = consultantService.consultantsAddFlag(onlineConsultants, id);

            return R.ok().put("data", result);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

    @GetMapping("/getConsultantById/{id}")
    public R getConsultantById(@PathVariable("id") Integer id) {
        try {
            ConsultantVo consultantVo = converter.getConsultantVo(id);
            return R.ok().put("data", consultantVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getConsultRecords/{id}")
    public R getConsultRecords(@PathVariable("id") Integer id) {
        try {
            List<ConsultRecord> records = consultRecordMapper.getConsultRecordByConsultantId(id);
            ArrayList<ConsultRecordVo> result = new ArrayList<>();
            for (ConsultRecord record : records) {
                result.add(converter.getConsultRecordVo(record.getConsultRecordId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getAvailableSupervisors/{id}")
    public R getAvailableSupervisors(@PathVariable("id") Integer id) {
        try {
            //有绑定关系的那些督导
            List<Supervisor> supervisors = supervisorMapper.getSupervisorByConsultantId(id);
            //筛选出在线的那些咨督导
            ArrayList<SupervisorVo> result = new ArrayList<>();
            for (Supervisor supervisor : supervisors) {
                Integer supervisorId = supervisor.getSupervisorId();
                if (consultantService.judgeOnline(supervisorId)) {
                    result.add(converter.getSupervisorVo(supervisorId));
                }
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getAllConsultants")
    public R getAllConsultants() {
        try {
            List<Consultant> consultants = consultantMapper.getAllConsultants();
            ArrayList<ConsultantVo> result = new ArrayList<>();
            for (Consultant consultant : consultants) {
                Integer consultantId = consultant.getConsultantId();
                result.add(converter.getConsultantVo(consultantId));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getAllOnlineConsultants")
    public R getAllOnlineConsultants() {
        try {
            List<Consultant> consultants = consultantMapper.getAllConsultants();
            //筛选出在线的
            ArrayList<ConsultantVo> result = new ArrayList<>();
            for (Consultant consultant : consultants) {
                Integer consultantId = consultant.getConsultantId();
                if (consultantService.judgeOnline(consultantId)) {
                    result.add(converter.getConsultantVo(consultantId));
                }
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getSupervisors/{id}")
    public R getSupervisors(@PathVariable("id") Integer id) {
        try {
            List<Supervisor> supervisors = supervisorMapper.getSupervisorByConsultantId(id);
            ArrayList<SupervisorVo> result = new ArrayList<>();
            for (Supervisor supervisor : supervisors) {
                Integer supervisorId = supervisor.getSupervisorId();
                result.add(converter.getSupervisorVo(supervisorId));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getDefaultDuty/{id}")
    public R getDefaultDuty(@PathVariable("id") Integer id) {

        try {

            HashMap<Object, Object> map = new HashMap<>();

            SetOperations<Object, Object> opsForSet = redisTemplate.opsForSet();

            ArrayList<Integer> defaultDuty = new ArrayList<>();
            for (int day = 1; day <= 7; day++) {
                if (opsForSet.isMember(String.format(CONSULTANT_DEFAULT_DUTY_KEY, day), id)) {
                    defaultDuty.add(day);
                }
            }

            map.put("defaultDuty", defaultDuty);

            return R.ok().put("data", map);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

    // 请求帮助
//    @JwtToken
    @PostMapping("/startHelp")
    public R startHelp(@RequestBody StartHelpVo vo) {
        return R.ok().put("data", consultantService.startHelp(vo));
    }

    // 结束帮助
//    @JwtToken
    @PostMapping("/endHelp")
    public R endHelp(@RequestBody EndHelpVo vo) {
        consultantService.endHelp(vo);
        return R.ok();
    }

    // 评论访客
//    @JwtToken
    @PostMapping("/commentConsult")
    public R commentConsult(@RequestBody CommentVisitorVo vo) {
       consultantService.commentVisitor(vo);
       return R.ok();
    }

    // 增加一个咨询师
//    @JwtToken
    @PostMapping("/add")
    public R add(@RequestBody ConsultantForm vo) {
        consultantService.addConsultant(vo);
        return R.ok();
    }

    // 修改绑定关系
//    @JwtToken
    @PostMapping("/changeSupervisor")
    public R changeSupervisor(@RequestBody ChangeSupervisorVo vo) {
        consultantService.changeSupervisor(vo);
        return R.ok();
    }

//    @JwtToken
    @PostMapping("/changeDefaultDuty")
    public R changeDefaultDuty(@RequestBody ChangeDefaultDutyVo vo) {
        consultantService.changeDefaultDuty(vo);
        return R.ok();
    }







}
